package org.liuchang.incomeAndExpenses;

import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JToolBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.JButton;

import org.liuchang.ext.DatePicker;
import org.liuchang.util.DBUtil;

public class Detail extends JPanel {
	private JTable table;

	/**
	 * Create the panel.
	 */
	public Detail() {
		setLayout(new BorderLayout(0, 0));
		
		JToolBar toolBar = new JToolBar();
		add(toolBar, BorderLayout.NORTH);
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);
		
		DatePicker dp = new DatePicker();
		toolBar.add(dp);
		
		table = new JTable();
		
		Vector<Vector<Object>> data = new Vector<Vector<Object>>();
		Vector<Object> d;
		
		int total = 0;
		
		Vector<String> title = new Vector<String>();
		title.add("日期");
		title.add("类型");
		title.add("名称");
		title.add("金额");
		
		DBUtil dbUtil = new DBUtil();
		ResultSet rs = dbUtil.executeQuery("SELECT P.DATE, T.NAME, P.NAME, P.MONEY FROM TBL_PAY P, TBL_TYPE T WHERE P.TYPE = T.ID AND MONTH(P.DATE)=MONTH(NOW()) ORDER BY P.DATE, P.ID");
		try {
			while (rs.next()) {
				d = new Vector<Object>();
				d.addElement(new String(rs.getString(1)));
				d.addElement(new String(rs.getString(2)));
				d.addElement(new String(rs.getString(3)));
				d.addElement(new Integer(rs.getInt(4)));
				total += rs.getInt(4);
				data.add(d);
			}
			d = new Vector<Object>();
			d.addElement(new String(""));
			d.addElement(new String("合计："));
			d.addElement(new String(""));
			d.addElement(new Integer(total));
			data.add(d);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
//		System.out.println(i);
		dbUtil.close();
		
		DefaultTableModel dtm = new DefaultTableModel(data, title);
		
		table.setModel(dtm);
//		table.setRowSorter(new TableRowSorter<DefaultTableModel>(dtm));
		
		DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
		renderer.setHorizontalAlignment(JLabel.CENTER);
		table.setDefaultRenderer(Object.class, renderer);
		
		scrollPane.setViewportView(table);
		
	}

}
